package Io;

import java.io.*;

//传输序列化后的对象进硬盘或者反序列化取出
public class Test10 {
    public static void main(String[] args) {
//        writeObject();
        readObject();
    }

    public static void writeObject(){
        try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\aa\\a\\b\\tt1.txt"));
            out.writeObject(new User(1,"顾新宇1"));
            out.writeObject(new User(2,"顾新宇2"));
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void readObject(){
        try {
            ObjectInputStream out = new ObjectInputStream(new FileInputStream("D:\\aa\\a\\b\\tt1.txt"));
            User u1 = (User)out.readObject();
            User u2 = (User)out.readObject();
            System.out.println(u1.toString()+"\t"+u2.toString());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
class User implements Serializable {
    private static final long serialVersionUID = 1699389372673409856L;
    private int id;
    private String name;

    public User() {
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}